package com.kwxy.hyt.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.kwxy.hyt.base.BaseController;
import com.kwxy.hyt.po.Fdy;
import com.kwxy.hyt.po.Manage;
import com.kwxy.hyt.po.Student;
import com.kwxy.hyt.po.Teacher;
import com.kwxy.hyt.service.FdyService;
import com.kwxy.hyt.service.ManageService;
import com.kwxy.hyt.service.StudentService;
import com.kwxy.hyt.service.TeacherService;

@Controller
@RequestMapping("/login")
public class LoginController  extends BaseController{
	
	@Autowired
	private ManageService manageService;
	@Autowired
	private FdyService fdyService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private StudentService studentService;
	
	/**
	 * 跳转登陆 管理员
	 * @return
	 */
	@RequestMapping("/mLogin")
	public String login(){
		return "login/mLogin";
	}
	
	/**
	 * 除了管理员意外的登陆
	 * @return
	 */
	@RequestMapping("/uLogin")
	public String uLogin(){
		return "login/login";
	}
	
	@RequestMapping("/message")
	public String message(Integer type,Model model){
		if (type == 1){
			model.addAttribute("mess", "该辅导员并没有负责的班级。");
		}
		return "login/message";
	}
	
	
	/**
	 * 管理员退出
	 * @param request
	 * @return
	 */
	@RequestMapping("/mtuichu")
	public String mtuichu(HttpServletRequest request){
		//request.getSession().invalidate();
		return "login/mLogin";
	}
	
	/**
	 * 欢迎页
	 * @return
	 */
	@RequestMapping("/welcome")
	private String welcome(){
		return "login/welcome";
	}
	
	/**
	 * 管理员登陆
	 * @param manage
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toMlogin")
	public String toLogin(Manage manage, HttpServletRequest request, HttpServletResponse response){
		Manage byEntity = manageService.getByEntity(manage);
		if(byEntity == null){
			return "redirect:/login/mtuichu";
		}else{
			request.getSession().setAttribute("role", 1);
			request.getSession().setAttribute("username", byEntity.getUsername());
			request.getSession().setAttribute("userId", byEntity.getId());
		}
		return "login/index";
	}
	
	
	/**
	 * 除了管理员意外的登陆
	 * @param code
	 * @param password
	 * @param role
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/tologin")
	public String toLogin(String code,String password,Integer role, HttpServletRequest request, HttpServletResponse response){
		
		// 4 学生，3 任课教师 ，2辅导员 
		
		if (role == 4){
			Student s = new Student();
			s.setIsDelete(0);
			s.setCode(code);
			s.setPassword(password);
			Student byEntity = studentService.getByEntity(s);
			if(byEntity == null){
				return "redirect:/login/uLogin.action";
			}else{
				request.getSession().setAttribute("role",role);
				request.getSession().setAttribute("username", byEntity.getUsername());
				request.getSession().setAttribute("userId", byEntity.getId());
				request.getSession().setAttribute("count", byEntity.getCount());
			}
		}
		if (role == 3){
			Teacher s = new Teacher();
			s.setIsDelete(0);
			s.setCode(code);
			s.setPassword(password);
			Teacher byEntity = teacherService.getByEntity(s);
			if(byEntity == null){
				return "redirect:/login/uLogin.action";
			}else{
				request.getSession().setAttribute("role",role);
				request.getSession().setAttribute("username", byEntity.getUsername());
				request.getSession().setAttribute("userId", byEntity.getId());
			}
		}
		if (role == 2){
			Fdy s = new Fdy();
			s.setCode(code);
			s.setPassword(password);
			Fdy byEntity = fdyService.getByEntity(s);
			if(byEntity == null){
				return "redirect:/login/uLogin.action";
			}else{
				request.getSession().setAttribute("role",role);
				request.getSession().setAttribute("username", byEntity.getUsername());
				request.getSession().setAttribute("userId", byEntity.getId());
			}
			
		}
		return "login/index";
	}
	
	/**
	 * 退出
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/tuichu")
	public String tuichu( HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		session.invalidate();
		return "login/uLogin";
	}
	
	/*
	 * 其他角色退出
	 */
	@RequestMapping("/uTui")
	public String uTui( HttpServletRequest request, HttpServletResponse response){
		HttpSession session = request.getSession();
		session.invalidate();
		return "redirect:/login/uLogin";
	}
	

	@RequestMapping("/head")
	private String head(){
		return "inc/head";
	}
	
	@RequestMapping("/left")
	private String left(){
		return "inc/left";
	}
	
}
